Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
The p-any package is a utility for handling multiple promises in JavaScript. It returns a promise that is fulfilled when any of the input promises are fulfilled, or rejected if all of the input promises are rejected.
Basic Usage
This feature demonstrates the basic usage of p-any. It takes an array of promises and returns a promise that resolves as soon as the first promise in the array resolves.
const pAny = require('p-any');
const promise1 = Promise.resolve('First');
const promise2 = new Promise((resolve) => setTimeout(resolve, 100, 'Second'));
const promise3 = new Promise((resolve, reject) => setTimeout(reject, 50, 'Third'));
pAny([promise1, promise2, promise3]).then(value => {
console.log(value); // 'First'
});
Handling Rejections
This feature demonstrates how p-any handles rejections. If all input promises are rejected, p-any returns an AggregateError.
const pAny = require('p-any');
const promise1 = new Promise((resolve, reject) => setTimeout(reject, 100, 'First'));
const promise2 = new Promise((resolve, reject) => setTimeout(reject, 200, 'Second'));
const promise3 = new Promise((resolve, reject) => setTimeout(reject, 300, 'Third'));
pAny([promise1, promise2, promise3]).catch(error => {
console.log(error); // AggregateError: All promises were rejected
});
The promise-any package provides similar functionality to p-any. It also returns a promise that resolves as soon as any of the input promises resolve, or rejects if all input promises are rejected. The main difference is that promise-any is a polyfill for the Promise.any method introduced in ES2021.
Bluebird is a fully-featured promise library that includes a method called Bluebird.any, which provides similar functionality to p-any. Bluebird offers additional features and optimizations for working with promises, making it a more comprehensive solution for promise management.
Wait for any promise to be fulfilled
Useful when you need the fastest promise.
You probably want this instead of Promise.race()
. Reason.
$ npm install p-any
Checks 3 websites and logs the fastest.
const got = require('got');
const pAny = require('p-any');
(async () => {
const first = await pAny([
got.head('https://github.com').then(() => 'github'),
got.head('https://google.com').then(() => 'google'),
got.head('https://twitter.com').then(() => 'twitter'),
]);
console.log(first);
//=> 'google'
})();
Returns a cancelable Promise
that is fulfilled when any promise from input
is fulfilled. If all the input
promises reject, it will reject with an AggregateError
error.
Type: Iterable<Promise|any>
Type: object
Type: Function
Receives the value resolved by the promise. Used to filter out values that doesn't satisfy a condition.
Exposed for instance checking.
FAQs
Wait for any promise to be fulfilled
The npm package p-any receives a total of 567,950 weekly downloads. As such, p-any popularity was classified as popular.
We found that p-any demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.